//index.js
//获取应用实例
const app = getApp()

const areaL = require("../../utils/area.js")
const Api = require("../../utils/api.js").API
const Store = require("../../utils/store.js").store
const addr = require('../../utils/address-parse.js')

Page({
    data: {
        areaL: [],
        list: {
            "addr": "",
            "area": "",
            "city": "",
            "ckGroupNo": "",
            "contactName": "",
            "contactPhone": "",
            "contactRemark": "",
            "province": "",
            "zipCode": ""
        },
        id:'',
        show: false,
        textValue: '',
        SSQ: '',
        tagMsg:'',
        cust:false,
        fenzhu:'true',
       
       
    },
    bindFormSubmit: function(e) {
        let value = e.detail.value.textarea;
        if (value != '') {
            addr.parseArea(addr.areaList);
            let parse_list = addr.parse(value);
            var arr;
            var bigAdress = parse_list.province + parse_list.city + parse_list.area;
            this.setData({
                [`list.addr`]: parse_list.addr,
                [`list.area`]: parse_list.area,
                [`list.ckGroupNo`]: '',
                [`list.city`]: parse_list.city,
                [`list.contactName`]: parse_list.name,
                [`list.contactPhone`]: parse_list.mobile,
                [`list.province`]: parse_list.province,
                SSQ: bigAdress,
              
               
            })
            // this.setData({
            //     list:parse_list
            // })
            console.log(parse_list);

        } else {
            wx.showToast({
                title: '请规范填写收件人信息!',
                icon: 'none'
            });
            return
        }

    },

    onLoad: function(op) {
        if(op.hasOwnProperty('fenzhu')){
            this.setData({
                fenzhu:op.fenzhu,
            })
            
        }
            
            
                if (op.hasOwnProperty('item')) {
                    if(op.item == '1'){
                    this.setData({
                        cust:true,
                        areaL: areaL.default,
                    })
                }else{
                    
                    
                
                    var dic = JSON.parse(op.item);
                    if(dic.cust == true){
                        this.setData({
                            cust:true,
                           
                        })
                    }
                    var b= dic.item;
                    
                      var bigAdress;
                         if(b.area == null && b.city == null && b.province == null){
                         bigAdress = ''     
                         }else{
                             bigAdress = areaL.default.province_list[b.province] + areaL.default.city_list[b.city] +
                                 areaL.default.county_list[b.area]
                         }
                   
                       
                    this.setData({
                        [`list.addr`]: b.addr,
                        [`list.area`]: b.area,
                         [`list.city`]: b.city,
                        [`list.ckGroupNo`]: dic.lxNo,
                        
                        [`list.contactName`]: b.contactName,
                        [`list.contactPhone`]: b.contactPhone,
                        [`list.province`]: b.province,
                          tagMsg:dic.lxName,
                        SSQ: bigAdress,
                        areaL: areaL.default,
                        id:b['id']
                
                    })
                }
                }
                 else {
                    this.setData({
                        
                        areaL: areaL.default,
                    })
                }
                   
            
      
    },
   onShow(){
      
   },
    onChangeName(event) {
        // event.detail 为当前输入的值
        console.log(event.detail);
        this.setData({

            [`list.contactName`]: event.detail,
        });
    },
    onChangePhone(event) {
        // event.detail 为当前输入的值
        console.log(event.detail);
        this.setData({

            [`list.contactPhone`]: event.detail,
        });
    },
    onChangeStreet(event) {
        // event.detail 为当前输入的值
        console.log(event.detail);
        this.setData({

            [`list.addr`]: event.detail,
        });
    },
    deleteZhi:function(e){
        let that = this;
        // let i = e.currentTarget.dataset.inde;
        //   let j = e.currentTarget.dataset.index;
          
        
          
         var str =that.data.id;
        Api.deleteByIdApi(str).then(res=>{
             wx.navigateBack();
           
        })
    }, 

    confirmArea: function(event) {
        var a = event.detail.values;
        console.log(a);
        var strArr = [];
        a.forEach(item => {
            strArr.push(item.name);
        })
        this.setData({
            SSQ: strArr.join(''),
            [`list.province`]: a[0].code,
            [`list.city`]: a[1].code,
            [`list.area`]: a[2].code,
            show: false
        })
    },
    cancelArea: function(e) {
        this.setData({
            show: false
        })
    },
    clickJump: function() {
        this.setData({
            show: true
        })
    },

    backList: function() {
        var data = this.data.list;
         
         
        if(this.data.fenzhu == 'false'){
          if(this.data.list.contactName == ''){
            return  wx.showToast({
                         title: '收货人姓名未填写!',
                         icon: 'none',
          })
          }else if(this.data.list.contactPhone == ''){
              return  wx.showToast({
                             title: '收货人联系电话未填写!',
                             icon: 'none',
              })
           }else if(this.data.list.addr == ''){
              return  wx.showToast({
                             title: '未选择收货人所在的省市区!',
                             icon: 'none',
              })
           }
           else if(this.data.list.addr == ''){
              return  wx.showToast({
                             title: '收货人详细地址未填写!',
                             icon: 'none',
              })
           }
        }else{
       
            if(this.data.list.contactName == ''){
              return  wx.showToast({
                           title: '收货人姓名未填写!',
                           icon: 'none',
            })
            }else if(this.data.list.contactPhone == ''){
                return  wx.showToast({
                               title: '收货人联系电话未填写!',
                               icon: 'none',
                })
             }else if(this.data.list.addr == ''){
                return  wx.showToast({
                               title: '未选择收货人所在的省市区!',
                               icon: 'none',
                })
             }else if(this.data.list.tagMsg == ''){
                return  wx.showToast({
                               title: '未选择收货人分组类型!',
                               icon: 'none',
                })
             }
             else if(this.data.list.addr == ''){
                return  wx.showToast({
                               title: '收货人详细地址未填写!',
                               icon: 'none',
                })
             }
          
        }

      let zhi = isNaN(parseInt(this.data.list.city,10));//return NAN     
    
      
        if(zhi){
        return  wx.showToast({
             title: '省市区信息请重新选择填写！',
              icon: 'none',
              duration: 2000
          })
          
        }
       
         if (this.data.id == "") {
            Api.saveInfoApi(data).then(res => {
                console.log(res);

                wx.navigateBack();
            })
        }else {
            data['id'] = this.data.id;
            Api.updateInfoApi(data).then(res => {
                console.log(res);

                wx.navigateBack();
            })
        }


    },

clickJump2:function(){
    
    wx.navigateTo({
        url:'../MyTags/index?type=1'
    })
    
}
});
